/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget;

import me.panpf.sketch.util.Utils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;

import java.util.List;

/**
 * FindEmptyView
 */
public class FindEmptyView extends Component implements Component.DrawTask {
    private List<Rect> fullRectList;
    private List<Rect> emptyRectList;
    private Rect boundsRect;

    private Paint boundsRectPaint;
    private Paint fullRectPaint;
    private Paint emptyRectPaint;

    public FindEmptyView(Context context) {
        this(context, null);
    }

    public FindEmptyView(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public FindEmptyView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        boundsRectPaint = new Paint();
        boundsRectPaint.setStyle(Paint.Style.STROKE_STYLE);
        boundsRectPaint.setColor(new Color(Color.getIntColor("#8800CD00")));
        boundsRectPaint.setStrokeWidth(Utils.vp2px(context, "1vp"));

        fullRectPaint = new Paint();
        fullRectPaint.setColor(new Color(Color.getIntColor("#88FF0000")));
        fullRectPaint.setStrokeWidth(Utils.vp2px(context, "1vp"));
        fullRectPaint.setStyle(Paint.Style.STROKE_STYLE);

        emptyRectPaint = new Paint();
        emptyRectPaint.setColor(new Color(Color.getIntColor("#880000CD")));
        emptyRectPaint.setStrokeWidth(Utils.vp2px(context, "1vp"));
        emptyRectPaint.setStyle(Paint.Style.STROKE_STYLE);

        addDrawTask(this::onDraw);
    }

    public void setBoundsRect(Rect boundsRect) {
        this.boundsRect = boundsRect;
    }

    public void setEmptyRectList(List<Rect> emptyRectList) {
        this.emptyRectList = emptyRectList;
    }

    public void setFullRectList(List<Rect> fullRectList) {
        this.fullRectList = fullRectList;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (fullRectList != null) {
            for (Rect fullRect : fullRectList) {
                if (!fullRect.isEmpty()) {
                    canvas.drawRect(fullRect.left * 3 + 1, fullRect.top * 3 + 1, fullRect.right * 3 - 1, fullRect.bottom * 3 - 1, fullRectPaint);
                }
            }
        }

        if (emptyRectList != null) {
            for (Rect emptyRect : emptyRectList) {
                if (!emptyRect.isEmpty()) {
                    canvas.drawRect(emptyRect.left * 3 + 1, emptyRect.top * 3 + 1, emptyRect.right * 3 - 1, emptyRect.bottom * 3 - 1, emptyRectPaint);
                }
            }
        }

        if (boundsRect != null && !boundsRect.isEmpty()) {
            canvas.drawRect(boundsRect.left * 3, boundsRect.top * 3, boundsRect.right * 3, boundsRect.bottom * 3, boundsRectPaint);
        }
    }
}
